#include <LiquidCrystalRus.h>
#include <EEPROM.h>

const int manual = 0;
const int v3_3 = 1;
const int v5 = 2;
const int v9 = 3;
const int v12 = 4;
const int v15 = 5;
const int v24 = 6;

int screen = 1; //1 - рабочий экран, 2 - Напряжение, 3 - Ток, 4 - Мощность
int mode = 1;  //1 - рабочий режим, 2 - установка напряжения, 3 - перегрузка
int voltage;
int restoreState;

void setup() {
  regulator_init();
  lcd_init();
  buttons_init();
  lcd_splash();
  voltage = regulator_getVoltage();
  restoreState = 0;
  if(regulator_isRestore()) restoreState = 1;
}

void loop() {
  buttons_handler();
  meters_handler();

  if(regulator_getState() and isOverdrive()) {
    regulator_off();
    mode=3;
    screen=100;
  }

   if(screen == 1) lcd_screen1();
   if(screen == 2) lcd_screen2();
   if(screen == 3) lcd_screen3();
   if(screen == 4) lcd_screen4();
   if(screen == 10) lcd_screen10(voltage);
   if(screen == 11) lcd_screen11(restoreState);
   if(screen == 100) lcd_screen100();
  
  if(mode == 1){
    if(button_isSet()) regulator_toggle();
  
    if(button_isRight()){
      screen++;
      lcd_swipeRight();
    }

    if(button_isLeft()){
      screen--;
      lcd_swipeLeft();
    }

    if(screen > 4) screen = 1;
    if(screen < 1) screen = 4;

    if(button_isSetLong()) {
      regulator_off();
      screen=10;
      mode=2;
      lcd_swipeRight();
    }

    if(button_isRightLong()) {
      voltage++;
      if(voltage > 6) voltage=6;
      regulator_set(voltage);
    }

    if(button_isLeftLong()) {
      voltage--;
      if(voltage < 0) voltage=0;
      regulator_set(voltage);
    }
  }else if(mode==2){
     if(screen == 10){
       if(button_isRight()) voltage++;
       if(button_isLeft()) voltage--;
  
       if(voltage < 0) voltage=0;
       if(voltage > 6) voltage=6;
     }else if(screen == 11){
       if(button_isRight()) restoreState++;
       if(button_isLeft()) restoreState--;
  
       if(restoreState < 0) restoreState=1;
       if(restoreState > 1) restoreState=0;
     }
     
     if(button_isSet()) {
        screen++;
        if(screen == 12) screen=10;
        lcd_swipeRight();
     }

     if(button_isSetLong()) {
        regulator_set(voltage);
        regulator_restoreSet(restoreState);
        screen=1;
        mode=1;
        lcd_swipeLeft();
     }
  }else if(mode==3){
    if(button_isSet()){
      screen=1;
      mode=1;
      lcd_swipeLeft();
    }
  }
  
  
}
